const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const extend = (deep, target, options) => {
  var copyIsArray;
  var toString = Object.prototype.toString;
  var hasOwn = Object.prototype.hasOwnProperty;

  var class2type = {
    '[object Boolean]': 'boolean',
    '[object Number]': 'number',
    '[object String]': 'string',
    '[object Function]': 'function',
    '[object Array]': 'array',
    '[object Date]': 'date',
    '[object RegExp]': 'regExp',
    '[object Object]': 'object'
  };

  var type = function (obj) {
    return obj == null ? String(obj) : class2type[toString.call(obj)] || "object";
  };

  var isWindow = function (obj) {
    return obj && typeof obj === "object" && "setInterval" in obj;
  };

  var isArray = Array.isArray || function (obj) {
    return type(obj) === "array";
  };

  var isPlainObject = function (obj) {
    if (!obj || type(obj) !== "object" || obj.nodeType || isWindow(obj)) {
      return false;
    }

    if (obj.constructor && !hasOwn.call(obj, "constructor")
      && !hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) {
      return false;
    }

    var key;
    for (key in obj) {
    }

    return key === undefined || hasOwn.call(obj, key);
  };
  for (var name in options) {
    var src = target[name];
    var copy = options[name];

    if (target === copy) {
      continue;
    }

    if (deep && copy
      && (isPlainObject(copy) || (copyIsArray = isArray(copy)))) {
      if (copyIsArray) {
        copyIsArray = false;
        var clone = src && isArray(src) ? src : [];

      } else {
        var clone = src && isPlainObject(src) ? src : {};
      }

      target[name] = extend(deep, clone, copy);
    } else if (copy !== undefined) {
      target[name] = copy;
    }
  }

  return target;
};
/**
 *
 * @param data 日期类型的对象（如：new Date()）
 * @param formatStr 要格式化的日期时间格式（如：“yyyy-MM-dd h:m:s”）
 * @returns {*}
 */
const dateFormat = (date, formatStr) => {
  var str = formatStr;
  var Week = ['日', '一', '二', '三', '四', '五', '六'];
  str = str.replace(/yyyy|YYYY/, date.getFullYear());
  str = str.replace(/yy|YY/, (date.getYear() % 100) > 9 ? (date.getYear() % 100).toString() : '0' + (date.getYear() % 100));
  str = str.replace(/MM/, (date.getMonth() + 1) > 9 ? (date.getMonth() + 1).toString() : '0' + (date.getMonth() + 1));
  str = str.replace(/M/g, (date.getMonth() + 1));
  str = str.replace(/w|W/g, Week[date.getDay()]);
  str = str.replace(/dd|DD/, date.getDate() > 9 ? date.getDate().toString() : '0' + date.getDate());
  str = str.replace(/d|D/g, date.getDate());
  str = str.replace(/hh|HH/, date.getHours() > 9 ? date.getHours().toString() : '0' + date.getHours());
  str = str.replace(/h|H/g, date.getHours());
  str = str.replace(/mm/, date.getMinutes() > 9 ? date.getMinutes().toString() : '0' + date.getMinutes());
  str = str.replace(/m/g, date.getMinutes());
  str = str.replace(/ss|SS/, date.getSeconds() > 9 ? date.getSeconds().toString() : '0' + date.getSeconds());
  str = str.replace(/s|S/g, date.getSeconds());
  return str
}
const Vertify = {
  //判断是否是一个标准url
  isURL: function (strUrl) {
    var regular = /^\b(((https?|ftp):\/\/)?[-a-z0-9]+(\.[-a-z0-9]+)*\.(?:com|edu|gov|int|mil|net|org|biz|info|name|museum|asia|coop|aero|[a-z][a-z]|((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d))\b(\/[-a-z0-9_:\@&?=+,.!\/~%\$]*)?)$/i;
    if (regular.test(strUrl)) {
      return true;
    }
    else {
      return false;
    }
  },
  isEmpty: function (str) {
    if (str == null || typeof str == "undefined" || ETools.string.trim(str) == "") {
      return true;
    } else {
      return false;
    }
  },
  //判断是否是数字
  isDigit: function (value) {
    var patrn = /^[0-9]*$/;
    if (patrn.exec(value) == null || value == "") {
      return false;
    } else {
      return true;
    }
  },
  //判断是否是固定电话
  isTelephone: function (phone) {
    var phone_reg = new RegExp(/^([+]{0,1}\d{3,4}|\d{3,4}-)?\d{7,8}$/);
    if (!phone_reg.test(phone)) {
      return false;
    }
    return true;
  },
  isMobile: function (mobile) {
    var mobile_reg = new RegExp(/^0{0,1}1[0-9]{10}$/);
    if (!mobile_reg.test(mobile)) {
      return false;
    }
    return true;
  },
  //QQ号码从10000开始
  isQQ: function (qq) {
    var qq_reg = new RegExp(/^[1-9][0-9]{4,}$/);
    if (!qq_reg.test(qq)) {
      return false;
    }
    return true;
  },
  //验证是否是邮箱
  isEmail: function (email) {
    var email_reg = new RegExp(/^\w+([-+.]\w+)*@\w+([-.]\w+)*.\w+([-.]\w+)*$/);
    if (!email_reg.test(email)) {
      return false;
    }
    return true;
  },
  //身份证
  isIDCard: function (str) {
    var IDCardReg = new RegExp(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/);
    if (!IDCardReg.test(str)) {
      return false;
    }
    return true;
  },
  //不带符号正整数
  isPlusDigit: function (digit) {
    var plusDigit_reg = new RegExp(/^\d+$/);
    if (!plusDigit_reg.test(digit)) {
      return false;
    }
    return true;
  },
  //中文字符
  isChinese: function (str) {
    if (str.length != 0) {
      var reg = /^[\u0391-\uFFE5]+$/;
      if (!reg.test(str)) {
        return false;
      }
    }
    return true;
  },
  //日期格式的字符串
  isDate: function (datestr) {
    var result = datestr.match(/((^((1[8-9]\d{2})|([2-9]\d{3}))(-)(10|12|0?[13578])(-)(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(-)(11|0?[469])(-)(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(-)(0?2)(-)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)(-)(0?2)(-)(29)$)|(^([3579][26]00)(-)(0?2)(-)(29)$)|(^([1][89][0][48])(-)(0?2)(-)(29)$)|(^([2-9][0-9][0][48])(-)(0?2)(-)(29)$)|(^([1][89][2468][048])(-)(0?2)(-)(29)$)|(^([2-9][0-9][2468][048])(-)(0?2)(-)(29)$)|(^([1][89][13579][26])(-)(0?2)(-)(29)$)|(^([2-9][0-9][13579][26])(-)(0?2)(-)(29)$))/);
    if (result == null) {
      return false;
    }
    return true;
  },
  //校验邮政编码
  isPostalCode: function (s) {
    //var patrn=/^[a-zA-Z0-9]{3,12}$/;
    var patrn = /^[a-zA-Z0-9 ]{3,12}$/;
    if (!patrn.exec(s)) {
      return false
    }
    return true
  },
  //校验登录名：只能输入5-20个以字母开头、可带数字、“_”、“.”的字串
  isRegisterUserName: function (s) {
    var patrn = /^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/;
    if (!patrn.exec(s)) return false
    return true
  },
  //校验用户姓名：只能输入1-30个以字母开头的字串
  isTrueName: function (s) {
    var patrn = /^[a-zA-Z]{1,30}$/;
    if (!patrn.exec(s)) return false
    return true
  },
  //校验密码：只能输入6-20个字母、数字、下划线
  isPassword: function (s) {
    var patrn = /^(\w){6,20}$/;
    if (!patrn.exec(s)) return false
    return true
  }
};
const trim = function (str) {
  return str.replace(/^(\s|\u00A0)+/, '').replace(/(\s|\u00A0)+$/, '');
};
module.exports = {
  formatTime: formatTime,
  extend: extend,
  dateFormat: dateFormat,
  Vertify: Vertify,
  trim: trim
}
